package com.busi.controller.indianamanage;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.busi.model.CompetitionUser;
import com.busi.util.DateUtil;
import com.busi.util.HttpUtils;
import com.busi.util.PropertyUtil;
import com.busi.util.StringUtil;
import com.google.gson.reflect.TypeToken;
import com.zcurd.controller.BaseController;

/**
 * Description:夺宝用户管理ctl
 * 
 * @author jiangrunchao
 * 
 */
public class UserManageController extends BaseController {

	public void listPage() {
		render("list.html");
	}

	/**
	 * 根据条件搜索用户
	 */
	public void listData() {
		CompetitionUser competitionUser = getBean(CompetitionUser.class, "");

		String body = gson.toJson(competitionUser);
		LOG.info("#listData() 调用夺宝用户管理搜索用户接口，请求报文:" + body);

		String baseurl = PropertyUtil.getStrValue("indiana.domain", "");
		String url = baseurl + "/emall/admin/usermanage/queryuser";
		String[][] headers = { { "Content-Type", "application/json" } };
		String resultJson = HttpUtils.doPost(url, body, headers, 3, 10, "utf-8");

		LOG.info("#listData() 调用夺宝用户管理搜索用户接口，响应报文:" + resultJson);
		renderJson(resultJson);

	}

	/**
	 * 导出excel
	 */
	public void exportCsv() throws UnsupportedEncodingException {
		CompetitionUser competitionUser = getBean(CompetitionUser.class, "");
		String body = gson.toJson(competitionUser);

		LOG.info("#exportCsv() 调用夺宝用户管理搜索用户接口，请求报文:" + body);

		String baseurl = PropertyUtil.getStrValue("indiana.domain", "");
		String url = baseurl + "/emall/admin/usermanage/exportxls";
		String[][] headers = { { "Content-Type", "application/json" } };
		String resultJson = HttpUtils.doPost(url, body, headers, 3, 10, "utf-8");

		LOG.info("#exportCsv() 调用夺宝用户管理搜索用户接口，响应报文:" + resultJson);

		List<CompetitionUser> userList = gson.fromJson(resultJson, new TypeToken<List<CompetitionUser>>() {
		}.getType());

		String filename = "夺宝用户明细" + DateUtil.getNowDate() + ".xls";
		setAttr("userList", userList);
		getResponse().setHeader("Content-disposition",
				"attachment; " + "filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
		render("export.html");

	}

	/**
	 * 异步：根据用户输入商品名称模糊查询商品信息
	 */
	public void queryGoodsName() {
		//String goodsName = getPara("goodsName");
		CompetitionUser competitionUser = getBean(CompetitionUser.class, "");
		String body = gson.toJson(competitionUser);
		
		LOG.info("#queryGoodsName() 调用夺宝用户模糊查询商品信息接口，请求报文：" + competitionUser.getGoodsName());
		
		String baseurl = PropertyUtil.getStrValue("indiana.domain", "");
		String url = baseurl + "/emall/admin/usermanage/querygoodsname";
		String[][] headers = { { "Content-Type", "application/json" } };
		String resultJson = HttpUtils.doPost(url,body, headers, 3, 10, "utf-8");
		LOG.info("#queryGoodsName() 调用夺宝用户管理模糊查询商品名称接口，响应报文:" + resultJson);
		if (StringUtil.isNotBlank(resultJson)) {
			renderJson(resultJson);
		}
	}

	/**
	 * 异步：查询夺宝期号
	 */
	public void queryDrawNum() {

		String baseurl = PropertyUtil.getStrValue("indiana.domain", "");
		String url = baseurl + "/emall/admin/usermanage/querydrawNum";
		String[][] headers = { { "Content-Type", "application/json" } };
		String resultJson = HttpUtils.doGet(url, headers, 3, 10, "utf-8");
		LOG.info("#queryDrawNum() 调用夺宝用户查询夺宝期号接口，响应报文:" + resultJson);

		renderJson(resultJson);
	}

	/**
	 * 根据用户id查看用户信息1
	 */
	public void queryUserInf() {

		String id = getPara("id");

		setAttr("id", id);

		render("userinfo.html");
	}

	/**
	 * 根据用户id查看用户信息2
	 */
	public void queryUserInfo() {
		String id = getPara("id");
		LOG.info("#queryDrawNum() 请求报文:" + id);
		
		String baseurl = PropertyUtil.getStrValue("indiana.domain", "");
		String url = baseurl + "/emall/admin/usermanage/queryuserinf/" + id;
		String[][] headers = { { "Content-Type", "application/json" } };
		String resultJson = HttpUtils.doGet(url, headers, 3, 10, "utf-8");

		LOG.info("#queryDrawNum() 调用夺宝用户-根据用户id查看用户信息接口，响应报文:" + resultJson);

		renderJson(resultJson);
	}

}
